package com.kongjs.application.controller.page;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kongjs.application.enums.PostType;
import com.kongjs.application.model.entity.Comments;
import com.kongjs.application.model.entity.Posts;
import com.kongjs.application.model.entity.TermRelationships;
import com.kongjs.application.service.ICommentsService;
import com.kongjs.application.service.IPostsService;
import com.kongjs.application.service.ITermRelationshipsService;
import com.kongjs.application.util.CommentsTreeBuilder;
import com.kongjs.common.rest.annotation.Page;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.comparator.Comparators;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@Page
@Controller
@RequestMapping("/articles")
public class ArticlePageController {
    @Resource
    private IPostsService postsService;
    @Resource
    private ITermRelationshipsService termRelationshipsService;
    @Resource
    private ICommentsService commentsService;

    @GetMapping
    public String index() {
        return "article/index";
    }

    @GetMapping("/{id}")
    public String getById(Model model, @PathVariable Long id) {
        Posts post = postsService.lambdaQuery().eq(Posts::getPostType, PostType.article.getValue()).eq(Posts::getId, id).one();
        model.addAttribute("post", post);
        if (!ObjectUtils.isEmpty(post)) {
            LambdaQueryWrapper<TermRelationships> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(TermRelationships::getObjectId, post.getId());
            List<TermRelationships> termRelationships = termRelationshipsService.list(wrapper);
            if (!CollectionUtils.isEmpty(termRelationships)) {
                List<Long> termIds = termRelationships.stream()
                        .sorted((o1, o2) -> Comparators.comparable().compare(o1, o2))
                        .map(TermRelationships::getTermTaxonomyId)
                        .toList();
                List<Posts> posts = postsService.lambdaQuery()
                        .eq(Posts::getPostType, PostType.article.getValue())
                        .in(Posts::getId, termIds)
                        .list();
                model.addAttribute("likePosts", posts);
            }
            //List<Comments> testComments = CommentsTreeBuilder.getTestComments();
            //List<Comments> comments1 = CommentsTreeBuilder.buildTree(testComments);
            List<Comments> comments = commentsService.lambdaQuery().eq(Comments::getCommentPostId, post.getId()).list();
            if (!CollectionUtils.isEmpty(comments)) {
                comments = CommentsTreeBuilder.buildTree(comments);
            }
            model.addAttribute("comments", comments);
        }
        return "article/index";
    }

    @PatchMapping("/{id}")
    public void action(@PathVariable Long id, @RequestParam String action) {
        Optional<Posts> optionalPosts = postsService.getOptById(id);
        if (optionalPosts.isPresent()) {
            Posts post = optionalPosts.get();
            if (action.equals("like")) {
                if (ObjectUtils.isEmpty(post.getPostLikes())) {
                    post.setPostLikes(0L);
                }
                post.setPostLikes(post.getPostLikes() + 1);
                postsService.updateById(post);
            } else if (action.equals("dislike")) {
                if (ObjectUtils.isEmpty(post.getPostDislikes())) {
                    post.setPostDislikes(0L);
                }
                post.setPostDislikes(post.getPostDislikes() + 1);
                postsService.updateById(post);
            }
        }
    }
}
